package com.example.software.controller;

import com.example.software.dto.TaskDTO;
import com.example.software.service.TaskService;
import com.example.software.vo.TaskVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/tasks")
public class TaskController {
    
    private static final Logger log = LoggerFactory.getLogger(TaskController.class);
    
    @Autowired
    private TaskService taskService;
    
    /**
     * 创建任务
     */
    @PostMapping
    public ResponseEntity<Long> createTask(@Valid @RequestBody TaskDTO taskDTO, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String userType = (String) request.getAttribute("userType");
        log.debug("接收到创建任务请求 - 用户ID: {}, 用户类型: {}", userId, userType);
        Long taskId = taskService.createTask(taskDTO, userId, userType);
        return ResponseEntity.ok(taskId);
    }
    
    /**
     * 获取任务列表
     */
    @GetMapping
    public ResponseEntity<List<TaskVO>> getTaskList(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        
        // 日志记录请求信息
        log.debug("获取任务列表请求 - 用户ID: {}", userId);
        
        try {
            // 即使用户ID为null也允许获取公开任务列表
            List<TaskVO> taskList = taskService.getTaskList(userId);
            return ResponseEntity.ok(taskList);
        } catch (Exception e) {
            log.error("获取任务列表时出错:", e);
            return ResponseEntity.status(500).body(new ArrayList<>());
        }
    }
    
    /**
     * 获取任务详情
     */
    @GetMapping("/{taskId}")
    public ResponseEntity<TaskVO> getTaskDetail(@PathVariable Long taskId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        TaskVO taskVO = taskService.getTaskDetail(taskId, userId);
        return ResponseEntity.ok(taskVO);
    }
    
    /**
     * 撤销任务
     */
    @PostMapping("/{taskId}/cancel")
    public ResponseEntity<Boolean> cancelTask(@PathVariable Long taskId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.cancelTask(taskId, userId);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 申请任务
     */
    @PostMapping("/{taskId}/apply")
    public ResponseEntity<Boolean> applyTask(@PathVariable Long taskId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.applyTask(taskId, userId);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 取消申请任务
     */
    @PostMapping("/{taskId}/cancel-apply")
    public ResponseEntity<Boolean> cancelApplyTask(@PathVariable Long taskId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.cancelApplyTask(taskId, userId);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 接受申请
     */
    @PostMapping("/applications/{applicationId}/accept")
    public ResponseEntity<Boolean> acceptApplication(@PathVariable Long applicationId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.acceptApplication(applicationId, userId);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 拒绝申请
     */
    @PostMapping("/applications/{applicationId}/reject")
    public ResponseEntity<Boolean> rejectApplication(@PathVariable Long applicationId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.rejectApplication(applicationId, userId);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 完成任务
     */
    @PostMapping("/{taskId}/complete")
    public ResponseEntity<Boolean> completeTask(@PathVariable Long taskId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.completeTask(taskId, userId);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 终止任务
     */
    @PostMapping("/{taskId}/terminate")
    public ResponseEntity<Boolean> terminateTask(@PathVariable Long taskId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.terminateTask(taskId, userId);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 零工标记任务为已完成
     */
    @PostMapping("/{taskId}/worker-complete")
    public ResponseEntity<Boolean> workerCompleteTask(@PathVariable Long taskId, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        boolean result = taskService.workerCompleteTask(taskId, userId);
        return ResponseEntity.ok(result);
    }
} 